//
//  BaseNavigationController.swift
//  Mediaplayer
//
//  Created by bin hu on 2025/7/8.
//

import UIKit
import SwiftTheme

class BaseNavigationController: UINavigationController,UINavigationControllerDelegate,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        //返回手势
        self.interactivePopGestureRecognizer?.delegate = self
        //导航栏背景
        self.navigationBar.setBackgroundImage(UIImage(ciImage: .white), for: .default)
        //去线
        self.navigationBar.shadowImage = UIImage.init()
        //字体颜色
        let dict = {[NSAttributedString.Key.foregroundColor:UIColor.black,NSAttributedString.Key.font:UIFont.systemFont(ofSize: 16)]}()
        self.navigationBar.titleTextAttributes = dict
        //        self.navigationBar.barStyle = .black
        self.delegate = self
        
        // ios15 导航栏变黑
        if #available(iOS 15.0, *) {
            let appearance = UINavigationBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.theme_backgroundColor = "Colors.navBar.backgroundColor"
            appearance.shadowColor = UIColor.clear
            appearance.theme_titleTextAttributes = ThemeStringAttributesPicker(keyPath: "Colors.navBar.textColor") { value -> [NSAttributedString.Key : AnyObject]? in
                guard let rgba = value as? String else {
                    return nil
                }
                
                let color = UIColor(rgba: rgba)
                let shadow = NSShadow(); shadow.shadowOffset = CGSize.zero
                let titleTextAttributes = [
                    NSAttributedString.Key.foregroundColor: color,
                    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16),
                    NSAttributedString.Key.shadow: shadow
                ]
                
                return titleTextAttributes
            }
            navigationBar.standardAppearance = appearance
            navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
        } else {
//            UINavigationBar.appearance().barTintColor = .white
//            UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.black]
//            UINavigationBar.appearance().tintColor = .white
            // navigation bar
            
            let navigationBar = UINavigationBar.appearance()
            navigationBar.theme_tintColor = "Colors.navBar.backgroundColor"
            navigationBar.theme_barTintColor = "Colors.black"
            navigationBar.theme_titleTextAttributes = ThemeStringAttributesPicker(keyPath: "Colors.navBar.textColor") { value -> [NSAttributedString.Key : AnyObject]? in
                guard let rgba = value as? String else {
                    return nil
                }
                
                let color = UIColor(rgba: rgba)
                let shadow = NSShadow(); shadow.shadowOffset = CGSize.zero
                let titleTextAttributes = [
                    NSAttributedString.Key.foregroundColor: color,
                    NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16),
                    NSAttributedString.Key.shadow: shadow
                ]
                
                return titleTextAttributes
            }
        }
        
    }
    
    // 重写此方法让 preferredStatusBarStyle 响应
    override var childForStatusBarStyle: UIViewController? {
        return self.topViewController
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.children.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            //添加图片
            // 创建自定义按钮
            let button = UIButton(type: .custom)
            button.theme_setImage("Icons.returnBackImage", forState: .normal)
            button.addTarget(self, action: #selector(leftClick), for: .touchUpInside)
            button.frame = CGRect(x: 0, y: 0, width: 36, height: 36) // 控制尺
            // 包装为 UIBarButtonItem
            let barButtonItem = UIBarButtonItem(customView: button)

//            let x = UIBarButtonItem(image: "HomePage.returnBackImage", style: .plain, target: self, action: #selector(leftClick))
//            if viewController is RPLookPictureViewController {
//               x.tintColor = .white
//            }else{
            button.theme_tintColor = "Colors.black"
//            }
            viewController.navigationItem.leftBarButtonItem = barButtonItem
            self.interactivePopGestureRecognizer?.isEnabled = true //self.viewControllers.count>1;
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
    }
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool){
        let hiddenVcs = ["HomePageViewController",
                         "ReaderViewController",
                         "ComicViewController",
                         "RPScanViewController",
                         "HotSearchViewController"]
        let identifier = NSStringFromClass(type(of: viewController)).components(separatedBy: ".").last ?? ""
        if hiddenVcs.contains(identifier) {
            navigationController.setNavigationBarHidden(true, animated: true)
        }else{
            if navigationController is UIImagePickerController {
                return
            }
            navigationController.setNavigationBarHidden(false, animated: true)
        }
    }
    
    @objc func leftClick() {
        if (self.presentedViewController != nil) {
            self.dismiss(animated: true, completion: nil)
        }else {
            self.popViewController(animated: true)
        }
    }

}
